home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / xfs / cache.h next >
Encoding:
C/C++ Source or Header  |  2008-10-04  |  4.0 KB  |  117 lines

  1. /*
  2.  * Copyright (c) 2006 Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License as
  7.  * published by the Free Software Foundation.
  8.  *
  9.  * This program is distributed in the hope that it would be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write the Free Software Foundation,
  16.  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  */
  18. #ifndef __CACHE_H__
  19. #define __CACHE_H__
  20.  
  21. #define    HASH_CACHE_RATIO    8
  22.  
  23. /*
  24.  * Cache priorities range from BASE to MAX.
  25.  *
  26.  * For prefetch support, the top half of the range starts at
  27.  * CACHE_PREFETCH_PRIORITY and everytime the buffer is fetched
  28.  * and is at or above this priority level, it is reduced to
  29.  * below this level (refer to libxfs_getbuf).
  30.  */
  31.  
  32. #define CACHE_BASE_PRIORITY    0
  33. #define CACHE_PREFETCH_PRIORITY    8
  34. #define CACHE_MAX_PRIORITY    15
  35.  
  36. /*
  37.  * Simple, generic implementation of a cache (arbitrary data).
  38.  * Provides a hash table with a capped number of cache entries.
  39.  */
  40.  
  41. struct cache;
  42. struct cache_node;
  43.  
  44. typedef void *cache_key_t;
  45.  
  46. typedef void (*cache_walk_t)(struct cache_node *);
  47. typedef struct cache_node * (*cache_node_alloc_t)(cache_key_t);
  48. typedef void (*cache_node_flush_t)(struct cache_node *);
  49. typedef void (*cache_node_relse_t)(struct cache_node *);
  50. typedef unsigned int (*cache_node_hash_t)(cache_key_t, unsigned int);
  51. typedef int (*cache_node_compare_t)(struct cache_node *, cache_key_t);
  52. typedef unsigned int (*cache_bulk_relse_t)(struct cache *, struct list_head *);
  53.  
  54. struct cache_operations {
  55.     cache_node_hash_t    hash;
  56.     cache_node_alloc_t    alloc;
  57.     cache_node_flush_t    flush;
  58.     cache_node_relse_t    relse;
  59.     cache_node_compare_t    compare;
  60.     cache_bulk_relse_t    bulkrelse;    /* optional */
  61. };
  62.  
  63. struct cache_hash {
  64.     struct list_head    ch_list;    /* hash chain head */
  65.     unsigned int        ch_count;    /* hash chain length */
  66.     pthread_mutex_t        ch_mutex;    /* hash chain mutex */
  67. };
  68.  
  69. struct cache_mru {
  70.     struct list_head    cm_list;    /* MRU head */
  71.     unsigned int        cm_count;    /* MRU length */
  72.     pthread_mutex_t        cm_mutex;    /* MRU lock */
  73. };
  74.  
  75. struct cache_node {
  76.     struct list_head    cn_hash;    /* hash chain */
  77.     struct list_head    cn_mru;        /* MRU chain */
  78.     unsigned int        cn_count;    /* reference count */
  79.     unsigned int        cn_hashidx;    /* hash chain index */
  80.     int            cn_priority;    /* priority, -1 = free list */
  81.     pthread_mutex_t        cn_mutex;    /* node mutex */
  82. };
  83.  
  84. struct cache {
  85.     unsigned int        c_maxcount;    /* max cache nodes */
  86.     unsigned int        c_count;    /* count of nodes */
  87.     pthread_mutex_t        c_mutex;    /* node count mutex */
  88.     cache_node_hash_t    hash;        /* node hash function */
  89.     cache_node_alloc_t    alloc;        /* allocation function */
  90.     cache_node_flush_t    flush;        /* flush dirty data function */
  91.     cache_node_relse_t    relse;        /* memory free function */
  92.     cache_node_compare_t    compare;    /* comparison routine */
  93.     cache_bulk_relse_t    bulkrelse;    /* bulk release routine */
  94.     unsigned int        c_hashsize;    /* hash bucket count */
  95.     struct cache_hash    *c_hash;    /* hash table buckets */
  96.     struct cache_mru    c_mrus[CACHE_MAX_PRIORITY + 1];
  97.     unsigned long long    c_misses;    /* cache misses */
  98.     unsigned long long    c_hits;        /* cache hits */
  99.     unsigned int         c_max;        /* max nodes ever used */
  100. };
  101.  
  102. struct cache *cache_init(unsigned int, struct cache_operations *);
  103. void cache_destroy(struct cache *);
  104. void cache_walk(struct cache *, cache_walk_t);
  105. void cache_purge(struct cache *);
  106. void cache_flush(struct cache *);
  107.  
  108. int cache_node_get(struct cache *, cache_key_t, struct cache_node **);
  109. void cache_node_put(struct cache *, struct cache_node *);
  110. void cache_node_set_priority(struct cache *, struct cache_node *, int);
  111. int cache_node_get_priority(struct cache_node *);
  112. int cache_node_purge(struct cache *, cache_key_t, struct cache_node *);
  113. void cache_report(FILE *fp, const char *, struct cache *);
  114. int cache_overflowed(struct cache *);
  115.  
  116. #endif    /* __CACHE_H__ */
  117.